package com.yyx.app1.MedicationAssistant_function.UI;

import android.app.Activity;
import android.app.Fragment;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.support.v7.app.AlertDialog;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.yyx.app1.MedicationAssistant_function.Controls.MedicationAssistant_Activity_Control.TreeListView.First_Second_ListViewAdapter;
import com.yyx.app1.MyApp;
import com.yyx.app1.R;
import com.yyx.app1.ui.BottomMenu_Fragment;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.FragmentById;
import org.androidannotations.annotations.ViewById;

/**
 * Created by yuexing on 2015/8/25.
 */

@EActivity(R.layout.activity_medicationassistant)
public class MedicationAssistant_Activity extends Activity{
    @ViewById
    ExpandableListView exlistview;

    @ViewById
    ImageView image_three_top,three_listview_left,top_menu_ico;

    @ViewById
    LinearLayout Layout_three;

    @ViewById
    ListView listview_three;

    @ViewById
    Button medicationassistant_search_button;

    @ViewById
    TextView three_listview_title;

    @FragmentById
    Fragment medicationassistant_fragment_bottommeun;

    public ListView getListview_three() {
        return listview_three;
    }

    @Click({R.id.image_three_top,R.id.three_listview_left})
    public void Togone()
    {
        Layout_three.setVisibility(View.GONE);
    }

    @Click
    public void medicationassistant_search_button()
    {
        startActivity(new Intent(MedicationAssistant_Activity.this, SearchMedication_Activity_.class));
    }

    @AfterViews
    public void show()
    {
        exlistview.setOverScrollMode(View.OVER_SCROLL_NEVER);
        listview_three.setOverScrollMode(View.OVER_SCROLL_NEVER);
        top_menu_ico.setImageBitmap(((MyApp) getApplication()).getBaseDataManager().getResDrawable(
                R.drawable.whitethreeline, top_menu_ico.getWidth(), top_menu_ico.getHeight()));
        ((BottomMenu_Fragment)medicationassistant_fragment_bottommeun).setShow("用药助手");
        final First_Second_ListViewAdapter adapter = new First_Second_ListViewAdapter(this);
        adapter.findAll();
        exlistview.setAdapter(adapter);
        exlistview.setGroupIndicator(null);
        Drawable drawable =new ColorDrawable(getResources().getColor(R.color.tree_divide));
        exlistview.setDivider(drawable);
        exlistview.setDividerHeight(1);
        exlistview.setChildDivider(drawable);
        exlistview.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
                Drawable drawable_2 = new ColorDrawable(Color.WHITE);
                drawable_2.setAlpha(0);
                image_three_top.setBackground(drawable_2);
                three_listview_left.setBackground(drawable_2);
                Layout_three.setVisibility(View.VISIBLE);
                three_listview_title.setText(adapter.getChild(groupPosition, childPosition).toString());
                three_listview_title.setTextColor(Color.WHITE);
                return true;
            }
        });
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        /**监听对话框里面的button点击事件*/
        DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener()
        {
            public void onClick(DialogInterface dialog, int which)
            {
                switch (which)
                {
                    case AlertDialog.BUTTON_POSITIVE:// "确认"按钮退出程序
                        finish();
                        break;
                    case AlertDialog.BUTTON_NEGATIVE:// "取消"第二个按钮取消对话框
                        break;
                    default:
                        break;
                }
            }
        };
        if (keyCode == KeyEvent.KEYCODE_BACK )
        {
            // 创建退出对话框
            AlertDialog isExit = new AlertDialog.Builder(this).create();
            // 设置对话框标题
            isExit.setTitle("您点击了退出");
            // 设置对话框消息
            isExit.setMessage("您确定真的要残忍的离开我吗?");
            // 添加选择按钮并注册监听
            isExit.setButton(DialogInterface.BUTTON_POSITIVE, "残忍的退出", listener);
            isExit.setButton(DialogInterface.BUTTON_NEGATIVE,"没想退出按错了", listener);
            // 显示对话框
            isExit.show();
        }
        return false;
    }
}
